package cn.foodProject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.foodProject.R;
import cn.foodProject.Utils.SpUtils;
import cn.foodProject.bean.MyOkhttp;
import cn.foodProject.bean.MyGson;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.AddCommentGsonBean;
import cn.foodProject.gsonBean.LoginGsonBean;
import okhttp3.Response;

//发表评论
public class AddCommentActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "AddCommentActivity";
    private EditText et_comment;
    private Button btn_OK;
    private Button btn_NO;
    private Toolbar toolbar_addcomment;
    private Context context = this;
    private int foodId;
    private String time;
    private LoginGsonBean.User user;
    private CommentActivity commentActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_comment);
        initView();

        Intent intent = getIntent();
        foodId =  intent.getIntExtra("foodId",-1);
        commentActivity = (CommentActivity) intent.getSerializableExtra("context");

        user = (LoginGsonBean.User) SpUtils.getBean(context,"user", LoginGsonBean.User.class);
    }

    private void initView() {
        et_comment = (EditText) findViewById(R.id.et_comment);
        btn_OK = (Button) findViewById(R.id.btn_OK);
        btn_NO = (Button) findViewById(R.id.btn_NO);

        btn_OK.setOnClickListener(this);
        btn_NO.setOnClickListener(this);
        toolbar_addcomment = (Toolbar) findViewById(R.id.toolbar_addcomment);

        //toolbar相关
        setSupportActionBar(toolbar_addcomment);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar_addcomment.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_OK:
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        submit();
                    }
                }.start();
                break;

            case R.id.btn_NO:
                finish();
                break;
        }
    }

    //同步请求发表评论
    private void submit() {
        //评论内容
        String comment = et_comment.getText().toString().trim();

        if(TextUtils.isEmpty(comment)){
            Toast.makeText(this, "comment不能为空", Toast.LENGTH_SHORT).show();
        }else {
            //获取当前时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            time = simpleDateFormat.format(date);

            //构建json数据
            JSONObject object = new JSONObject();
            try {
                object.put("foodid",foodId);
                object.put("comment",comment);
                object.put("postdate",time);
                object.put("userid",user.getId());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.d(TAG, "submit: JSON内容"+object.toString());

            try {
                Response response = MyOkhttp.getResponse(MyUrl.getUrl("/comment/add"), object.toString());
                if (response.isSuccessful()) {
                    String s = response.body().string();
                    AddCommentGsonBean addCommentGsonBean = (AddCommentGsonBean) MyGson.fromJson(s,AddCommentGsonBean.class);

                    if(addCommentGsonBean.isSuccess()){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context,"发表成功",Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(AddCommentActivity.this,CommentActivity.class);
                                intent.putExtra("foodId", foodId);
                                startActivity(intent);
                                finish();
                            }
                        });
                    }else{
                        Log.d(TAG, "submit: "+addCommentGsonBean.getMessage());
                    }
                } else {
                    Log.d(TAG, "onFailure: 请求失败");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
